package cn.edu.gsau.info.service.impl;


import cn.edu.gsau.info.dao.LunwenDao;
import cn.edu.gsau.info.models.Lunwen;
import cn.edu.gsau.info.models.Shenhe;
import cn.edu.gsau.info.models.User;
import cn.edu.gsau.info.models.Zhengmingcailiao;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.*;
import cn.edu.gsau.info.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @BelongsProject: gsau
 * @BelongsPackage: cn.edu.gsau.info.service.impl
 * @Author: yq
 * @CreateTime: 2022-11-19
 * @Description: 论文的业务实现类
 * @Version: 1.0
 */
@Service
public class LunwenServiceImpl extends ServiceImpl<LunwenDao, Lunwen> implements ILunwenService {
    @Autowired
    LunwenDao lunwenDao;

    @Autowired
    IZhengmingcailiaoService zhengmingcailiaoService;

    @Autowired
    IShenheService shenheService;

    @Autowired
    IUserService userService;


    @Autowired
    IReviewService reviewService;

    private Integer id = ModuleIdClass.LUNWEN_id;

    @Override
    public List<Shenhe> selectShenhe(Long recoreId){
        QueryWrapper<Shenhe> qw=new QueryWrapper<>();
        qw.eq("recoreId",recoreId);
        Shenhe one = shenheService.getOne(qw);
        List<Shenhe> list=new ArrayList<>();
        list.add(one);
        return list;
    }

    /**
     * @description:审核
     * @author: yq
     * @date:  2022-11-30
     * @param: 实体类
     * @return: 集合
     **/
    @Override
    public List<Shenhe> updateShenhe(Shenhe shenhe) {
        Date date=new Date();

        QueryWrapper<Shenhe> qw=new QueryWrapper<>();
        qw.eq("recoreId",shenhe.getRecoreId());
       // shenhe.setModuleId(id);
        qw.eq("moduleId",id);
        qw.eq("memo",0);
        Shenhe one = shenheService.getOne(qw);
        System.out.println(one);


       //one.setModuleId(shenhe.getModuleId());
        one.setShenHeTime(date);
        one.setModuleId(id);
        one.setShenHeResult(shenhe.getShenHeResult());
        one.setMemo("1");
        boolean b = shenheService.updateById(one);

        Lunwen lunwen=new Lunwen();
        lunwen.setNode("审核"+shenhe.getNode());
        if(lunwen.getNode().equals("审核已通过")){
            lunwen.setStatus("流转完成");
        }


        lunwen.setId(shenhe.getRecoreId());
        lunwenDao.updateById(lunwen);

        List<Shenhe> list=new ArrayList<>();
        list.add(one);
        return list;
    }

    /**
     * @description: 论文更新
     * @author: yq
     * @date:
     * @param:
     * @return:
     **/
    @Override
    public List<Lunwen> updateLunwen(Lunwen lunwen) {
        System.out.println(lunwen.toString());
        List list = new ArrayList();
        if(lunwen.getNode()=="审核未通过"){
            int j = lunwenDao.updateById(lunwen);
            if (!lunwen.getNode().equals("审核已通过")) {
                User user = new User();
                user.setUserName("yangqiang");
                if(lunwen.getNode()=="审核未通过"){
                    lunwen.setNode("待审核");
                }
                FileSave fileSave[] = lunwen.getFileList();

                if (fileSave != null) {
                    for (int i = 0; i < fileSave.length; i++) {
                        Zhengmingcailiao zhengmingcailiao = new Zhengmingcailiao();
                        zhengmingcailiao.setModuleId(ModuleIdClass.LUNWEN_id);
                        zhengmingcailiao.setRecoreId(lunwen.getId());
                        zhengmingcailiao.setFileName(fileSave[i].getOldfilename());
                        zhengmingcailiao.setFileFullPath(user.getUserName() + "/" + fileSave[i].getNewfilename());
                        boolean save = zhengmingcailiaoService.save(zhengmingcailiao);
                    }
                }
                list.add(lunwen);
            }
        }
        return list;
    }

    /**
     * @description: 论文查询
     * @author: yq
     * @date: 2022-11-19
     * @param: id
     * @return: list集合
     **/
    @Override
    public List<Lunwen> selectLunwen(Long id) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("tianbaoId", id);
        List<Lunwen> lunwens = lunwenDao.selectList(qw);
        return lunwens;
    }

    /**
     * @description: 单条论文记录查询
     * @author: yq
     * @date: 2022-11-19
     * @param: id
     * @return: list集合
     **/
    @Override
    public Lunwen selectOne(long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return lunwenDao.selectOne(queryWrapper);
    }

    /**
     * @description:
     * @author: yq
     * @date: 2022-11-26
     * @param: 论文实体类
     * @return: 集合
     **/
    @Override
    public List<Lunwen> addInfo(Lunwen lunwen) {

        lunwen.setTianbaoId(5L);
        Date date = new Date();
        lunwen.setCreateTime(date);
        lunwen.setUpdateTime(date);
        lunwen.setStatus("进行中");
        lunwen.setNode("待审核");
        Review byId1 = reviewService.getById(lunwen.getTianbaoId());
        byId1.getUserId();
        User byId2 = userService.getById(byId1.getUserId());
        lunwen.setManager(byId2.getNickName());

        User user = new User();
        user.setUserName("yangqiang");
        List list = new ArrayList<>();
        int insert = lunwenDao.insert(lunwen);
        System.out.println(lunwen.getNode());

        FileSave fileSave[] = lunwen.getFileList();

        if (fileSave != null) {
            for (int i = 0; i < fileSave.length; i++) {
                Zhengmingcailiao zhengmingcailiao = new Zhengmingcailiao();
                zhengmingcailiao.setModuleId(ModuleIdClass.LUNWEN_id);
                zhengmingcailiao.setRecoreId(lunwen.getId());
                zhengmingcailiao.setFileName(fileSave[i].getOldfilename());
                zhengmingcailiao.setFileFullPath(user.getUserName() + "/" + fileSave[i].getNewfilename());
                boolean save = zhengmingcailiaoService.save(zhengmingcailiao);
            }
        }
        list.add(lunwen);

        Shenhe shenhe = new Shenhe();
        shenhe.setModuleId(ModuleIdClass.LUNWEN_id);
        shenhe.setRecoreId(lunwen.getId());
        shenhe.setTijiaoName(lunwen.getTianbaoName());
        shenhe.setTijiaoTime(date);
        shenhe.setTijiaoId(lunwen.getTianbaoId());
        if(lunwen.getDesc()!=null){
            shenhe.setTijiaoMemo(lunwen.getDesc());
        }

        Review review = reviewService.getById(lunwen.getTianbaoId());
        User byId = userService.getById(review.getUserId());
        shenhe.setShenHeName(byId.getNickName());
        shenhe.setShenheId(review.getUserId());


        boolean save = shenheService.save(shenhe);

        return list;
    }

    /**
     * @description: 通过状态查询
     * @author: yq
     * @date: 2022-11-24
     * @param: 审核状态
     * @return: list集合
     **/
    @Override
    public List<Lunwen> search(Paramss params) {
        HashMap<String, String> map = new HashMap<>();
        map.put("guanjianzi", params.getGuanjianzi());
        map.put("status", params.getStatus());
        IPage<Lunwen> ipage = new Page<Lunwen>(params.getPage(), 5);
        QueryWrapper qw = new QueryWrapper();
        qw.like(map.get("guanjianzi") != null && !map.get("guanjianzi").equals(""), "name", params.getGuanjianzi());
        qw.like(map.get("status") != null && !map.get("status").equals(""), "status", params.getStatus());
        IPage page = lunwenDao.selectPage(ipage, qw);
        List list = new ArrayList<>();
        list.add(page);
        return list;
    }

    /**
     * @description: 查询需审核的论文
     * @author: yq
     * @date: 2022-11-29
     * @param:
     * @return: 集合
     **/
    @Override
    public List<Lunwen> searchExamine(Paramex paramex) {
        HashMap<String, String> map = new HashMap<>();
        map.put("guanjianzi", paramex.getGuanjianzi());
        IPage<Lunwen> ipage = new Page<>(paramex.getPage(), 5);
        QueryWrapper qw = new QueryWrapper();
        qw.like(map.get("guanjianzi") != null && !map.get("guanjianzi").equals(""), "name", paramex.getGuanjianzi());
        qw.eq("node","待审核");
        IPage page = lunwenDao.selectPage(ipage, qw);
        List list = new ArrayList<>();
        list.add(page);
        return list;
    }

    /**
     * @description:查询相关证明材料
     * @author: yq
     * @date: 2022-11-21
     * @param: id1：模板类型id，id2：某一条记录的id
     * @return: list集合
     **/
    @Override
    public List<Zhengmingcailiao> findZhengMingCaiLiao(Long id2) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("moduleId", id);
        qw.eq("recoreId", id2);
        List list = zhengmingcailiaoService.list(qw);

        return list;
    }


}
